package tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


public class PagingTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;

	private String url; // 分页查询的url
	
	private int currPage; // 目标页
	
	private int pageSize = 10; // 每页显示的记录数，默认为10条
	
	private int totalPages; // 总页数
	
	private int totalRecords; // 总记录数
	
	private boolean isAppoint = false; // 是否可跳转至指定页面，默认不显示
	
	@Override
	public int doStartTag() throws JspException {
		
		if(url == null || url.equals("")) {
			return SKIP_BODY;
		}
		
		url += url.indexOf("?") > -1 ? "&" : "?";
		
		// 计算总页数
		if(totalRecords > 0) {
			totalPages = totalRecords % pageSize == 0 
				? (totalRecords / pageSize)
				: (totalRecords / pageSize + 1);
		} else {
			return SKIP_BODY;
		}
		
		// 目标页面的范围在[1, totalPages]
		currPage = currPage / pageSize + 1;
		currPage = (currPage < 1) ? 1 : (currPage > totalPages) ? totalPages : currPage;
		
		StringBuilder sb = new StringBuilder();
		sb.append("<div id='paging_root'><div id='paging_style'>");
		
		// 目标页面【pageIndex】与每页显示数量【pageSize】的参数名称暂时来自于PaginationVO，考虑可自定义
		// 目标页面大于3，显示首页
		if(currPage > 3) {
			sb.append("<a name='paging_item'  href='" + url + "pageIndex=0&pageSize=" + pageSize + "'>首页</a>");
		}
		// 目标页面大于1，显示上一页
		if(currPage > 1) {
			sb.append("<a name='paging_item'  href='" + url
					+ "pageIndex=" + ((currPage-2)*pageSize) + "&pageSize=" + pageSize + "'>上一页</a>");
		}
		
		// 显示以当前显示页尾中心的前后各2页，总计5页
		int start = -2;
		while (start <= 2) {
			int page = currPage + start;
			
			if(page >= 1 && page <= totalPages) {
				if(page == currPage) {
					sb.append("<span name='paging_current'>" + page + "</span>");
				} else {
					sb.append("<a name='paging_item'  href='" + url
							+ "pageIndex=" + ((page-1)*pageSize) + "&pageSize=" + pageSize + "'>" + page + "</a>");
				}
			}
			
			start++;
		}
		
		// 目标页面小于总页数时，显示下一页
		if(currPage < totalPages) {
			sb.append("<a name='paging_item'  href='" + url
					+ "pageIndex=" + (currPage*pageSize) + "&pageSize=" + pageSize + "'>下一页</a>");
		}
		// 目标小于总页数-2时，显示尾页
		if(currPage < totalPages - 2) {
			sb.append("<a name='paging_item'  href='" + url
					+ "pageIndex=" + ((totalPages-1)*pageSize) + "&pageSize=" + pageSize + "'>尾页</a>");
		}
		sb.append("<span id='paging_total' value='" + totalPages + "'>共 " + totalPages + " 页</span>");
		
		// 是否显示指定跳转按钮
		if(isAppoint) {
			sb.append("<span id='paging_go_text'>到第<input type='text' id='paging_text'>页</span>");
			sb.append("<a id='paging_go_btn' ps='" + pageSize + "' href='" + url + "pageSize=" + pageSize + "'>确定</a>");
		}
		
		sb.append("<span style='margin-left: 50px'>共"+totalRecords+"条</div></div>");
		
		// 添加样式
		/*sb.append("<script>(function(){");
		
		sb.append("$('#paging_root').attr('class', 'bbs_pages');");
		sb.append("$('#paging_style').attr('class', 'pub_page');");
		sb.append("$(\"[name='paging_current']\").attr('class', 'current');");
		sb.append("$(\"[name='paging_item']\").each(function(){$(this).attr('class', 'item');});");
		sb.append("$('#paging_total').attr('class', 'total');");
		
		if(isAppoint) {
			sb.append("$('#paging_go_text').attr('class', 'go-text');");
			sb.append("$('#paging_text').attr('class', 'text');");
			sb.append("$('#paging_go_btn').attr('class', 'btn-go');");
			
			// 添加事件
			// 添加到达指定页面事件
			sb.append("$('#paging_go_btn').click(function(event){");
			sb.append("var appoint=$('#paging_text').val();");
			sb.append("if(isNaN(appoint)) {event.preventDefault();} else {");
			sb.append("if(appoint<1 || appoint>" + totalPages + ")");
			sb.append("{event.preventDefault();} else {");
			sb.append("var href=$(this).attr('href') + '&pageIndex=' + ((appoint-1) * 5);");
			sb.append("$(this).attr('href', href);");
			
		}
		
		sb.append("})();</script>");*/
		
		JspWriter writer = this.pageContext.getOut();
		try {
			writer.print(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return EVAL_BODY_INCLUDE;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}

	public void setIsAppoint(boolean isAppoint) {
		this.isAppoint = isAppoint;
	}

}
